// 引入vuex
import store from '../store/index.js'

// 引入router
import router from '../router/index.js'

export const loginController = {
	// 登录
	login(curThis){
		let curUser = curThis.user;
		// 验证
		if(curUser.uname === '' || curUser.pwd === ''){
			curThis.$toast({
				message:'账号密码不能为空',
				type:'fail'
			})
			return;
		}else {
			let users = store.state.users;
			let res = users.filter((item)=>{
				return item.phone === curUser.phone && item.pwd === curUser.pwd;
			})
			
			if(res.length !== 0){
				console.log(curThis.$route.params.toPage);
				let path = curThis.$route.params.toPage.name;
				let params = curThis.$route.params.toPage.params;
				router.push({name:path,params:params});
				store.commit({
					type:'setLoginUser',
					curUser:res[0]
				})
			}else{
				curThis.$toast({
					message:'账号密码不匹配',
					type:'fail'
				})
			}
		}
		
	}
}